package com.sailer.practice.guava.thread;

import com.google.common.util.concurrent.*;
import com.sailer.practice.guava.thread.config.ThrearPoolConfig;
import com.sailer.practice.model.WorkParam;
import com.sailer.practice.model.Worker;
import org.apache.commons.lang3.time.StopWatch;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


public class ListenableFutureTest {

    public static void main(String[] args) {
        int taskCount=200;
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<String> ls = new ArrayList<>();
        ListeningExecutorService pool = MoreExecutors.listeningDecorator(new ThrearPoolConfig().jobExecutor());
        for (int i=0;i<taskCount;i++) {
            final ListenableFuture<String> future = pool.submit(new Worker(new WorkParam(Double.parseDouble(String.valueOf(i)), "这是第" + i + "个任务")));
            Futures.addCallback(future, new FutureCallback<String>() {
                @Override
                public void onSuccess(String result) {
                    System.out.println(result);
                    ls.add(result);
                }

                @Override
                public void onFailure(Throwable t) {
                    t.printStackTrace();
                }
            });
        }
        System.out.println("@@@@任务提交完成");
        StopWatch watch=new StopWatch();
        watch.start();
        while (taskCount!=ls.size()){
            System.out.println(ls.size());
        }
        watch.stop();
        System.out.println("@@@@   done :"+watch.getNanoTime()/1000000L);
        System.exit(0);

    }
}
